﻿// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)

using System;
using System.IO;
using System.Xml;

namespace ICSharpCode.WixBinding
{
	public class WixBinaryElement : WixElementBase
	{
		public const string BinaryElementName = "Binary";
		
		public WixBinaryElement(WixDocument document) 
			: base(BinaryElementName, document)
		{
		}
		
		public string Source {
			get {
				if (HasAttribute("SourceFile")) {
					return GetAttribute("SourceFile");
				} 
				return GetAttribute("src");
			}
		}
		
		/// <summary>
		/// Gets the full filename including the root directory path to this binary.
		/// </summary>
		/// <remarks>
		/// The full path is generated by using the WixDocument's filename.
		/// </remarks>
		public string GetFileName()
		{
			string relativeFileName = WixPropertyParser.Parse(Source, OwnerWixDocument);
			return GetFullPath(relativeFileName);
		}
			
		string GetFullPath(string relativeFileName)
		{
			string documentFileName = OwnerWixDocument.FileName;
			if (!String.IsNullOrEmpty(documentFileName)) {
				return Path.Combine(Path.GetDirectoryName(documentFileName), relativeFileName);
			}
			return relativeFileName;
		}
	}
}
